package com.yami.shop.personnft.common.listener;

import cn.hutool.extra.mail.MailException;
import com.yami.shop.bean.model.NftTransferRecord;
import com.yami.shop.common.config.RocketMqConstant;
import com.yami.shop.common.util.Json;
import com.yami.shop.personnft.common.service.NftTransferRecordService;
import com.yami.shop.personnft.common.service.PersonNftService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * 转赠
 * @author FrozenWatermelon
 * @date 2021/1/7
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.NFT_OTHER_SHIFT,consumerGroup = RocketMqConstant.NFT_OTHER_SHIFT)
@Slf4j
public class NftOhterConsumer implements RocketMQListener<Map<String,String>> {

    @Autowired
    private PersonNftService personNftService;
    @Autowired
    private RocketMQTemplate stockMqTemplate;

    @Autowired
    private NftTransferRecordService nftTransferRecordService;
    /**
     * 转增
     * @param map
     */
    @SneakyThrows
    @Override
    public void onMessage(Map<String,String> map) {
        log.info("nft转出到中间库... message: " + Json.toJsonString(map));
        //转出人的助计词
        String mnemonic = map.get("mnemonic");
        String address = map.get("address");

        String denomId = map.get("denomId");
        //tokenid
        String nftId = map.get("nftId");
        //目标地址
        String reci = map.get("reci");
        String userId = map.get("userId");

        String type = map.get("type");//1转增2 兑换 3合成

//        //中间库地址
        if(ObjectUtils.isEmpty(denomId)||ObjectUtils.isEmpty(mnemonic) ||ObjectUtils.isEmpty(nftId)){
            log.info("nft 资产转出中间库参数不足... message:reci: "+reci+"denomId :"+denomId
                    +"nftId:"+nftId);
            throw new MailException("nft 资产转出中间库异常");
        }

        NftTransferRecord record = new NftTransferRecord();
        record.setDenomId(denomId);
        record.setNftId(nftId);
        record.setReci(reci);
        record.setUserId(userId);
        record.setMnemonic(mnemonic);
        record.setSenderAddress(address);
        Integer iType = 0;
        switch (type) {// 商城目前没有转增， 兑换也是不会删除的，没有合成
            case "1":
                iType = 3;//转增添加
                break;
            case "2":
                iType = 5;//兑换删除
                break;
            case "3":
                iType = 6; //合成删除
        }
        record.setType(iType);
        nftTransferRecordService.save(record);
        log.info("NFT转赠 已保存到待划转【reci:" + reci+ "denomId:" +denomId+ "nftId:" + nftId + "】");

    }

}
